# 101.对称二叉树
# 给你一个二叉树的根节点root ， 检查它是否轴对称。
#
# 示例1：
# 输入：root = [1, 2, 2, 3, 4, 4, 3]
# 输出：true
#
# 示例2：
# 输入：root = [1, 2, 2, null, 3, null, 3]
# 输出：false


# Definition for a binary tree node.
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
class Solution:
    def isSymmetric(self, root: TreeNode) -> bool:
        # 大致能看懂含义，但是还是要仔细琢磨
        def com(left,right):
            if left != None and right == None:
                return False
            elif left == None and right != None:
                return False
            elif not left and not right:
                return True
            elif left.val != right.val:
                return False
            # 此时就是：左右节点都不为空，且数值相同的情况
            # 此时才做递归，做下一层的判断
            outside = com(left.left, right.right)
            inside = com(left.right,right.left)
            isSame= outside and inside
            return isSame
        if not root:
            return True
        return com(root.left,root.right)


if __name__ == '__main__':
    a31 = TreeNode(3)
    a32 = TreeNode(4)
    a33 = TreeNode(4)
    a34 = TreeNode(3)
    a21 = TreeNode(2,a31,a32)
    a22 = TreeNode(2,a33,a34)
    a11 = TreeNode(1,a21,a22)
    tmp = Solution()
    res = tmp.isSymmetric(a11)
    print(res)
    # 这里先不上传其他写法，层序的和我想的有差距，先不写了
